<template>
  <div id="view-artManager">
    <div class="tableControl m15">
      <el-button type="primary" icon="el-icon-minus" @click="batchDelArticle" :disabled="!selection.length">批量删除</el-button>
    </div>
    <div class="articleList m15">
      <table-field :config="config" ref="articleList" @selection-change="selectionChange">
        <el-table-column type="selection" width="55" fixed="left" align="center">
        </el-table-column>
        <el-table-column label="封面" prop="cover">
          <template scope="item">
            <img v-if="!$chy.isEmpty(item.row.cover)" class="acticleCover" :src="baseImgUrl + item.row.cover" alt="">
            <div v-else>--</div>
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title" min-width="200px"></el-table-column>
        <el-table-column label="描述" prop="description" min-width="120px">
          <template scope="item">
            <popover-field :value="item.row.description" popMaxWidth="500px" />
          </template>
        </el-table-column>
        <el-table-column label="作者" prop="userName"></el-table-column>
        <el-table-column label="浏览量" prop="browseNum"></el-table-column>
        <el-table-column label="排序" prop="sort" align="center"></el-table-column>
        <el-table-column label="创建时间" prop="createTime" min-width="180px" align="center"></el-table-column>
        <el-table-column label="更新时间" prop="updateTime" min-width="180px" align="center"></el-table-column>
        <el-table-column fixed="right" label="操作" align="center" min-width="120px">
          <template slot-scope="scope">
            <el-tooltip content="编辑">
              <el-button class="el-icon-edit" @click="handleEdit(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除">
              <el-button class="el-icon-delete" @click="handleDel(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </table-field>
    </div>
    <drawer-field :visible.sync="isShowDrawer" :title="drawerTitle" @confirm="confirm">
      <el-form :model="articleData" :rules="rules" ref="articleForm" label-width="auto" class="" :key="key">
        <el-form-item label="封面" prop="cover">
          <upload-field v-model="articleData.cover" />
        </el-form-item>
        <el-form-item label="标题" prop="title" v-if="!isEdit">
          <el-input v-model="articleData.title" @input="change($event)"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="articleData.description" @input="change($event)"></el-input>
        </el-form-item>
        <!-- <el-form-item label="作者" prop="userName">
          <el-input v-model="articleData.userName"></el-input>
        </el-form-item> -->
        <el-form-item label="排序" prop="sort">
          <el-input v-model.number="articleData.sort"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <div class="editor">
            <editor-field v-model="articleData.content" height="calc(100% - 121px)" />
          </div>
        </el-form-item>
      </el-form>
    </drawer-field>
    <addActicle-field @success="success"/>
  </div>
</template>

<script>
import TableField from '@/components/TableField'
import DrawerField from '@/components/DrawerField'
import PopoverField from '@/components/PopoverBoxField'
import EditorField from '@/components/EditorField'
import UploadField from '@/components/UploadField'
import AddActicleField from '@/components/AddActicleField/index.vue'
import { updateArtApi, deleteArtApi } from '@/api/article'
export default {
  name: 'view-artManager',
  components: {
    [TableField.name]: TableField,
    [DrawerField.name]: DrawerField,
    [PopoverField.name]: PopoverField,
    [EditorField.name]: EditorField,
    [UploadField.name]: UploadField,
    [AddActicleField.name]: AddActicleField
  },
  data() {
    return {
      baseImgUrl: process.env.VUE_APP_DOWNLOAD_IMG_API,
      config: {
        url: '/article/manageList',
        rootProperty: 'list',
        data: {
        }
      },
      isShowDrawer: false,
      drawerTitle: '',
      isEdit: false,
      key: 0,
      selection: [],
      rules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'change' }
        ]
      },
      articleData: {}
    }
  },
  watch: {
    isShowDrawer(newVal) {
      newVal === false || this.key++
    }
  },
  mounted() {
    this.getArtList()
  },
  methods: {
    getArtList(isReload) {
      this.$refs.articleList.loadData(isReload)
    },
    success() { 
      this.getArtList(true)
    },
    batchDelArticle() {
      this.handleDel(this.selection)
    },
    selectionChange(selection) {
      this.selection = selection
    },
    handleEdit(row) {
      this.isShowDrawer = true
      this.articleData = $chy.cloneDeep(row)
    },
    handleDel(row) {
      let params = null
      if (Array.isArray(row)) {
        params = row.map(ele => {
          return {
            id: ele.artId
          }
        })
      } else {
        params = [{ id: row.artId }]
      }
      this.$confirm('将删除文章?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteArtApi(params).then(res => {
          if (res.code === 1) {
            this.$message({
              type: 'success',
              message: '删除文章成功!'
            })
          } else {
            this.$message({
              type: 'error',
              message: '删除文章失败!'
            })
          }
          this.getArtList()
        })
      }).catch(err => {
        console.log(err)
      })
    },
    confirm() {
      updateArtApi(this.articleData).then(res => {
        if (res.code === 1) {
          this.$message({
            type: 'success',
            message: '修改成功'
          })
        } else {
          this.$message({
            type: 'error',
            message: res.msg
          })
        }
        this.isShowDrawer = false
        this.getArtList()
      })
    },
    change() {
      this.$forceUpdate()
    }

  }
}
</script>

<style lang="scss" scoped>
#view-artManager {
  .acticleCover {
    width: 100px;
    height: 50px;
  }
}

.editor {
  height: 400px;
}
</style>